public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void  put(int key, int value) {
       //先判断这个数存在不
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.value=value;
               return;
            }
            cur=cur.next;
        }
        //不存在的时候，添加
        //判断此时的负载因子是否大于0.7
        if (loadFactor()>=0.75f){
            //扩容
            resize();
        }
       Node node=new Node(key,value);
        node.next=array[index];
        array[index]=node;
        size++;
    }
 
 
    private void resize() {
       Node [] arr=new Node[2*array.length];
        for (int i = 0; i <array.length ; i++) {
            Node cur=array[i];
            while(cur!=null){
                int index=cur.key%arr.length;
                Node curr=cur.next;
                cur.next=arr[index];
                arr[index]=cur;
                cur=curr;
            }
        }
        array=arr;
    }
 
 
    private double loadFactor() {
        return size * 1.0 / array.length;
    }
 
 
    public HashBucket() {
            array=new Node[DEFAULT_SIZE];
    }
 
 
    public int get(int key) {
        int index=key%array.length;
        Node cur=array[index];
        while (cur!=null){
            if(cur.key==key){
                return cur.value;
            }
            cur=cur.next;
        }
        return -1;
    }
}
